package com.sitech.portal.server.dao.ibatis;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.portal.server.dao.IWorkPlanDao;

public class WorkPlanDaoImpl extends BaseIbnmsDao implements IWorkPlanDao {

	private static final List<String> columns = Arrays.asList("TASK_ID", "TASK_NAME", "TASK_DESC", "PARENT_ID", "START_TIME", "END_TIME", "TASK_STATUS", "COMPLETION_TIME", "COMPLETION_DESC", "MILESTONE", "LOGIN_ID", "ALARM_MODE", "ALARM_MINUTE");
    private static final List<String> cols_date = Arrays.asList("START_TIME", "END_TIME", "COMPLETION_TIME");

    /**
     * 增加一个工作计划
     * @param wp 
     */
    public Map insertWorkPlan(Map<String, String> wp) {
        wp.put("TASK_ID", UUID.randomUUID().toString().toUpperCase());
        StringBuilder vals = new StringBuilder();
        StringBuilder cols = new StringBuilder();
        for (int i = 0; i < columns.size(); i++) {
            String colkey = columns.get(i);
            String colvalue = wp.get(colkey);
            if (colvalue != null) {
                colvalue = colvalue.replaceAll("'", "''");
                //如果是日期
                if (cols_date.contains(colkey)) {
                    vals.append(",to_date('").append(colvalue).append("','yyyy-mm-dd hh24:mi:ss')");
                } else {
                    vals.append(",'").append(colvalue).append("'");
                }
                cols.append(",").append(colkey);
            }
        }
        String sql = "insert into TB_SYS_WORKPLAN (" + cols.substring(1) + ") values(" + vals.substring(1) + ")";
        this.insert("insertWorkPlan", sql);
        return wp;
    }

    /**
     * 删除工作计划
     * @param wpid
     */
    public void delWorkPlan(String wpid) {
        this.delete("delWorkPlan", wpid);
    }

    /**
     * 修改工作计划
     * @param wp
     */
    public void updateWorkPlan(Map<String, String> wp) {
        StringBuilder sql = new StringBuilder();
        for (int i = 1; i < columns.size(); i++) {
            if (wp.containsKey(columns.get(i))) {
                String val = wp.get(columns.get(i));
                if (val != null) {
                    val = val.replaceAll("'", "''");
                    //如果是日期
                    if (cols_date.contains(columns.get(i))) {
                        sql.append(", ").append(columns.get(i)).append("=to_date('").append(val).append("','yyyy-mm-dd hh24:mi:ss') ");
                    } else {
                        sql.append(", ").append(columns.get(i)).append("='").append(val).append("' ");
                    }
                }
            }
        }
        sql.append(" where TASK_ID = '").append(wp.get("TASK_ID").replaceAll("'", "''")).append("'");
        sql.delete(0, 1).insert(0, "update TB_SYS_WORKPLAN set ");
        System.out.println("sql = " + sql);
        this.update("updateWorkPlan", sql.toString());
    }

    /**
     * 查询所有的工作计划
     */
    public List findAllWorkPlan(String loginId, String date) {
        Map param = new HashMap();
        param.put("DATE", date);
        param.put("LOGIN_ID", loginId);
        return this.query("findAllWorkPlan", param);
    }

    /**
     * 查询计划所有的步骤
     * @param pid
     * @return
     */
    public List findWorkPlanByParentId(String pid) {
        return this.query("findWorkPlanByParentId", pid);
    }

    /**
     * 查询计划所有的步骤
     * @param pid
     * @return
     */
    public Map findWorkPlanById(String TASK_ID) {
        return (Map) this.query("findWorkPlanById", TASK_ID).get(0);
    }

}
